/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AutoProvMode;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning.EagerAutoProvisionScheduler;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapEntry;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AutoProvisionEager
/*     */   extends AutoProvision
/*     */ {
/*     */   private Provisioning.EagerAutoProvisionScheduler scheduler;
/*     */   
/*     */   private AutoProvisionEager(LdapProv prov, Domain domain, Provisioning.EagerAutoProvisionScheduler scheduler)
/*     */   {
/*  52 */     super(prov, domain);
/*  53 */     this.scheduler = scheduler;
/*     */   }
/*     */   
/*     */   static void handleScheduledDomains(LdapProv prov, Provisioning.EagerAutoProvisionScheduler scheduler) {
/*  57 */     ZLdapContext zlc = null;
/*     */     
/*     */     try
/*     */     {
/*  61 */       Server localServer = prov.getLocalServer();
/*  62 */       String[] scheduledDomains = localServer.getAutoProvScheduledDomains();
/*     */       
/*  64 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.AUTO_PROVISION);
/*     */       
/*  66 */       for (String domainName : scheduledDomains) {
/*  67 */         if (scheduler.isShutDownRequested()) {
/*  68 */           ZimbraLog.autoprov.info("eager auto provision aborted"); return;
/*     */         }
/*     */         
/*     */         try
/*     */         {
/*  73 */           Domain domain = prov.get(Key.DomainBy.name, domainName);
/*  74 */           if (domain == null) {
/*  75 */             ZimbraLog.autoprov.info("EAGER auto provision: no such domain " + domainName);
/*     */ 
/*     */           }
/*     */           else
/*     */           {
/*     */ 
/*  81 */             prov.reload(domain, true);
/*     */             
/*  83 */             if (!autoProvisionEnabled(domain))
/*     */             {
/*     */ 
/*     */ 
/*     */ 
/*  88 */               ZimbraLog.autoprov.info("Domain %s is scheduled for EAGER auto provision but EAGER mode is not enabled on the domain.  Removing domain %s from %s on server %s", new Object[] { domain.getName(), domain.getName(), "zimbraAutoProvScheduledDomains", localServer.getName() });
/*     */               
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  97 */               localServer.removeAutoProvScheduledDomains(domain.getName());
/*     */             }
/*     */             else
/*     */             {
/* 101 */               ZimbraLog.autoprov.info("Auto provisioning accounts on domain %s", new Object[] { domainName });
/* 102 */               AutoProvisionEager autoProv = new AutoProvisionEager(prov, domain, scheduler);
/* 103 */               autoProv.handleBatch(zlc);
/*     */             }
/* 105 */           } } catch (Throwable t) { if ((t instanceof OutOfMemoryError)) {
/* 106 */             Zimbra.halt("Ran out of memory while auto provision accounts", t);
/*     */           } else {
/* 108 */             ZimbraLog.autoprov.warn("Unable to auto provision accounts for domain %s", domainName, t);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (ServiceException e) {
/* 114 */       ZimbraLog.autoprov.warn("Unable to auto provision accounts", e);
/*     */     } finally {
/* 116 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private void handleBatch(ZLdapContext zlc) throws ServiceException {
/* 121 */     if (!autoProvisionEnabled(this.domain)) {
/* 122 */       throw ServiceException.FAILURE("EAGER auto provision is not enabled on domain " + this.domain.getName(), null);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 127 */       if (!lockDomain(zlc)) {
/* 128 */         ZimbraLog.autoprov.info("EAGER auto provision unable to lock domain: skip domain " + this.domain.getName() + " on server " + this.prov.getLocalServer().getName());
/*     */       }
/*     */       else
/*     */       {
/* 132 */         createAccountBatch(); }
/*     */     } finally {
/* 134 */       unlockDomain(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   Account handle() throws ServiceException
/*     */   {
/* 140 */     throw new UnsupportedOperationException();
/*     */   }
/*     */   
/*     */   private static boolean autoProvisionEnabled(Domain domain) {
/* 144 */     return domain.getMultiAttrSet("zimbraAutoProvMode").contains(ZAttrProvisioning.AutoProvMode.EAGER.name());
/*     */   }
/*     */   
/*     */   private void createAccountBatch() throws ServiceException
/*     */   {
/* 149 */     long polledAt = System.currentTimeMillis();
/*     */     
/* 151 */     List<AutoProvision.ExternalEntry> entries = new ArrayList();
/* 152 */     boolean hitSizeLimitExceededException = searchAccounts(entries, this.domain.getAutoProvBatchSize());
/* 153 */     ZimbraLog.autoprov.info("%d external LDAP entries returned as search result", new Object[] { Integer.valueOf(entries.size()) });
/* 154 */     int stuckAcctNum = 0;
/* 155 */     for (AutoProvision.ExternalEntry entry : entries) {
/* 156 */       if (this.scheduler.isShutDownRequested()) {
/* 157 */         ZimbraLog.autoprov.info("eager auto provision aborted");
/* 158 */         return;
/*     */       }
/*     */       try
/*     */       {
/* 162 */         ZAttributes externalAttrs = entry.getAttrs();
/* 163 */         String acctZimbraName = mapName(externalAttrs, null);
/*     */         
/* 165 */         ZimbraLog.autoprov.info("auto creating account in EAGER mode: " + acctZimbraName + ", dn=\"" + entry.getDN() + "\"");
/* 166 */         Account acct = createAccount(acctZimbraName, entry, null, ZAttrProvisioning.AutoProvMode.EAGER);
/* 167 */         if (acct == null) {
/* 168 */           stuckAcctNum++;
/*     */         }
/*     */       }
/*     */       catch (ServiceException e) {
/* 172 */         ZimbraLog.autoprov.warn("unable to auto create account, dn=\"" + entry.getDN() + "\"", e);
/* 173 */         stuckAcctNum++;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 179 */     if ((hitSizeLimitExceededException) && (entries.size() == stuckAcctNum)) {
/* 180 */       ZimbraLog.autoprov.info("search result contains unsuccessful external entries, increasing batch size by %d", new Object[] { Integer.valueOf(stuckAcctNum) });
/* 181 */       int currentBatchSize = this.domain.getAutoProvBatchSize();
/* 182 */       this.domain.setAutoProvBatchSize(currentBatchSize + stuckAcctNum);
/* 183 */       ZimbraLog.autoprov.info("batch size is %d now", new Object[] { Integer.valueOf(this.domain.getAutoProvBatchSize()) });
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 199 */     if (!hitSizeLimitExceededException) {
/* 200 */       String lastPolledAt = DateUtil.toGeneralizedTimeWithMs(new Date(polledAt));
/* 201 */       ZimbraLog.autoprov.info("Auto Provisioning has finished for now, setting last polled timestamp: " + lastPolledAt);
/* 202 */       this.domain.setAutoProvLastPolledTimestampAsString(lastPolledAt);
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean lockDomain(ZLdapContext zlc) throws ServiceException {
/* 207 */     Server localServer = this.prov.getLocalServer();
/*     */     
/* 209 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().domainLockedForEagerAutoProvision();
/* 210 */     Map<String, Object> attrs = new HashMap();
/* 211 */     attrs.put("zimbraAutoProvLock", localServer.getId());
/*     */     
/* 213 */     boolean gotLock = this.prov.getHelper().testAndModifyEntry(zlc, ((LdapEntry)this.domain).getDN(), filter, attrs, this.domain);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 218 */     this.prov.reload(this.domain, true);
/* 219 */     ZimbraLog.autoprov.debug("lock domain %s", new Object[] { gotLock ? "successful" : "failed" });
/*     */     
/* 221 */     return gotLock;
/*     */   }
/*     */   
/*     */   private void unlockDomain(ZLdapContext zlc) throws ServiceException
/*     */   {
/* 226 */     Map<String, Object> attrs = new HashMap();
/* 227 */     attrs.put("zimbraAutoProvLock", "");
/*     */     
/* 229 */     this.prov.getHelper().modifyAttrs(zlc, ((LdapEntry)this.domain).getDN(), attrs, this.domain);
/*     */     
/*     */ 
/*     */ 
/* 233 */     this.prov.reload(this.domain, true);
/* 234 */     ZimbraLog.autoprov.debug("domain unlocked");
/*     */   }
/*     */   
/*     */   private boolean searchAccounts(final List<AutoProvision.ExternalEntry> entries, int batchSize) throws ServiceException
/*     */   {
/* 239 */     String lastPolledAt = this.domain.getAutoProvLastPolledTimestampAsString();
/* 240 */     String[] returnAttrs = getAttrsToFetch();
/*     */     
/* 242 */     SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor(false)
/*     */     {
/*     */       public void visit(String dn, IAttributes ldapAttrs) throws SearchLdapOptions.StopIteratingException
/*     */       {
/* 246 */         entries.add(new AutoProvision.ExternalEntry(dn, (ZAttributes)ldapAttrs));
/*     */       }
/*     */       
/* 249 */     };
/* 250 */     boolean hitSizeLimitExceededException = AutoProvision.searchAutoProvDirectory(this.prov, this.domain, null, null, lastPolledAt, returnAttrs, batchSize, visitor, true);
/*     */     
/*     */ 
/* 253 */     ZimbraLog.autoprov.debug("searched external LDAP source, hit size limit ? %s", new Object[] { Boolean.valueOf(hitSizeLimitExceededException) });
/* 254 */     return hitSizeLimitExceededException;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/AutoProvisionEager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */